package com.orion;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.util.CharsetUtil;

/**
 * @author li.lc
 */
public class TestByteBuf4 {
    public static void main(String[] args) {
        ByteBuf byteBuf = Unpooled.copiedBuffer("hello world", CharsetUtil.UTF_8);
        System.out.println("byteBuf的容量为：" + byteBuf.capacity());
        System.out.println("byteBuf的可读容量为：" + byteBuf.readableBytes());
        System.out.println("byteBuf的可写容量为：" + byteBuf.writableBytes());
        byteBuf.clear(); //重置readerIndex 、 writerIndex 为0
        System.out.println("byteBuf的容量为：" + byteBuf.capacity());
        System.out.println("byteBuf的可读容量为：" + byteBuf.readableBytes());
        System.out.println("byteBuf的可写容量为：" + byteBuf.writableBytes());

        ByteBuf byteBuf2 = Unpooled.copiedBuffer("hello world", CharsetUtil.UTF_8);
        int capacity = byteBuf2.readableBytes();
        while (capacity > 0){
            System.out.println(byteBuf2.readByte());
            capacity--;
        }

        int capacity2= byteBuf2.readableBytes();
        System.out.println("capacity2="+capacity2);
        while (capacity2 > 0){
            System.out.println(byteBuf2.readByte());
            capacity2--;
        }
    }
}
/**
 * 运行结果：
 *
 * byteBuf的容量为：33
 byteBuf的可读容量为：11
 byteBuf的可写容量为：22
 byteBuf的容量为：33
 byteBuf的可读容量为：0
 byteBuf的可写容量为：33
 104
 101
 108
 108
 111
 32
 119
 111
 114
 108
 100
 capacity2=0
 *
 */
